﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using UnityEngine;
using UnityEngine.Playables;
using Yoozoo.Core.Common;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class GuideEventPreliminary : LatentActionNode
    {
        public BBParameter<bool> enableCityTouchInput = true;

        public BBParameter<bool> backToMainUI = false;

        public BBParameter<string> resetScene;

        public BBParameter<float> waitTime;

        private bool stopped = false;
        private bool finish = false;
        
        public override bool allowRoutineQueueing { get { return false; } }

        public override IEnumerator Invoke()
        {
            if (backToMainUI !=null && backToMainUI.value)
            {
                stopped = false;
                BlueprintUtils.ShowMainUi(OnStopped);
                yield return new UnityEngine.WaitUntil(() =>
                {
                    return stopped;
                });
            }
            
            yield return new WaitForOneFrame();

            if (resetScene != null && !string.IsNullOrEmpty(resetScene.value))
            {
                if (!BlueprintUtils.IsInScene(resetScene.value))
                {
                    finish = false;
                    //需要优先判定当前是否在加载新的场景中
                    yield return new UnityEngine.WaitUntil(() =>
                    {
                        return LoadingUtils.LoadingTaskManager.enterSceneSuccess;
                    });
            
                    BlueprintUtils.ResetScene(resetScene.value,OnFinish);
                    yield return new UnityEngine.WaitUntil(() =>
                    {
                        return finish;
                    });
                }
            }
            
            yield return new WaitForOneFrame();

            BlueprintUtils.SwitchCityTouchInput(enableCityTouchInput.value);

            if (waitTime.value > 0)
            {
                yield return new WaitForSeconds(waitTime.value);
            }

        }
        
        private void OnStopped(PlayableDirector director)
        {
            stopped = true;
        }
        
        private void OnFinish()
        {
            finish = true;
        }
    }
}